//
//  BaseViewController.m
//  Kfangchan
//
//  Created by liran on 14/11/5.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "BaseViewController.h"
#import "SDImageCache.h"
#import "LoginViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.opHandlers = [[NSMutableArray alloc] initWithCapacity:3];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goLogin:) name:@"TokenFailed" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"TokenFailed" object:nil];
}

- (void)goLogin:(BOOL)animated {
    theApp.reloadBuildingList = YES;
    theApp.reloadCustomerList = YES;
    theApp.reloadCommissionList = YES;
    LoginViewController *lvc = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:lvc];
    [self presentViewController:nav animated:animated completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}

- (void)cacelAllRequest
{
    if (self.opHandlers) {
        for (MKNetworkOperation *op in self.opHandlers) {
            DLog(@"baseEngine release = %@",op.url);
            [[BaseEngine sharedEngine] cancelOperation:op];
        }
    }
    [self.opHandlers removeAllObjects];
}

- (void)dealloc
{
    DLog(@"Controller dealloc = %@",NSStringFromClass([self class]));
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self cacelAllRequest];
    showIndicator(NO, nil);
}

@end
